/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package App;

import generated.BoardType;
import generated.CardType;
import generated.PositionType;
import java.util.Random;
import utils.BoardTypeAnalyser;

/**
 *
 * @author lars.kloeser
 */
public class RandomMoveUtils {
    public static PositionType calcShiftCard(PositionType forbidden){
        Random rand = new Random();
        int sideOfBoard = rand.nextInt(4);
        int slotOnSide = (rand.nextInt(3))*2 + 1;
        PositionType res = new PositionType();
        if(sideOfBoard == 0){
            res.setCol(0);
            res.setRow(slotOnSide);
        }
        else if(sideOfBoard == 1){
            res.setCol(slotOnSide);
            res.setRow(0);
        }
        else if(sideOfBoard == 2){
            res.setCol(6);
            res.setRow(slotOnSide);
        }
        else if(sideOfBoard == 3){
            res.setCol(slotOnSide);
            res.setRow(6);
        }
        if(res.equals(forbidden)) return calcShiftCard(forbidden);
        return res;
    }
    public static PositionType calcPlayerPosition(PositionType playerPos, BoardType board){
       PositionType newPos = new PositionType();
       int col = playerPos.getCol();
       int row = playerPos.getRow();
       newPos.setCol(col + 1);
       newPos.setRow(row);
       if(testMove(newPos, playerPos, board)) return newPos;
       
       newPos.setCol(col - 1);
       newPos.setRow(row);
       if(testMove(newPos, playerPos, board)) return newPos;
       
       newPos.setCol(col);
       newPos.setRow(row + 1);
       if(testMove(newPos, playerPos, board)) return newPos;
       
       newPos.setCol(col);
       newPos.setRow(row -1);
       if(testMove(newPos, playerPos, board)) return newPos;
       
       return playerPos;
    }
    private static boolean testMove(PositionType move,PositionType prevMove, BoardType board){
        if(move.getCol() == -1 || move.getRow() == -1) return false;
        CardType card = BoardTypeAnalyser.getCard(move.getRow(), move.getCol(), board);
        if(move.getCol() == prevMove.getCol() && move.getRow() == prevMove.getRow()+1){
            return card.getOpenings().isLeft();
        }
        if(move.getCol() == prevMove.getCol() && move.getRow() == prevMove.getRow()-1){
            return card.getOpenings().isRight();
        }
        if(move.getCol() == prevMove.getCol() +1 && move.getRow() == prevMove.getRow()){
            return card.getOpenings().isTop();
        }
        if(move.getCol() == prevMove.getCol()-1 && move.getRow() == prevMove.getRow()+1){
            return card.getOpenings().isBottom();
        }
        return false;
    }
}
